<template>
  <div>
    <a-card :bordered='false'>

      <f-search-form :more='true' ref='searchs' @searchHandler='searchHandler' :tableObj='$refs.table'>
        <f-search-form-item label='单位' name='customId' type='select' :data='api.custom.list' valueKey='customId' label-key='customName' />
        <f-search-form-item label='部门' ref='dept' name='deptId' type='select' :data='api.hyorder.deptList' valueKey='deptId' label-key='deptName' />
        <f-search-form-item label='单号' name='expenseNo' />
        <f-search-form-item label='申请开始日期' type='date' name='beginDate' />
        <f-search-form-item label='申请结束日期' type='date' name='endDate' />
      </f-search-form>

      <f-table ref='table' rowKey='expenseId' :columns='columns' :multiple='false' :operator='operator'
               :params='queryParam' :api='api.expense' handler=''>
         <span slot='paymentType' slot-scope='text, record, index'>
          <div v-if="text=='PT_01'">采购预付款</div>
          <div v-if="text=='PT_02'">快递费</div>
          <div v-if="text=='PT_03'">销售费用</div>
          <div v-if="text=='PT_04'">其他</div>
          <div v-if="text=='PT_05'">市场保证金</div>
          <div v-if="text=='PT_06'">开票税金</div>
        </span>
        <span slot='checkState' slot-scope='text, record, index'>
            <a-tag color='pink' v-if='text==0'>未提交</a-tag>
            <a-tag color='#87d068' v-if='text==1' @click='processImg(record)'>{{ record.taskName }}</a-tag>
            <a-tag color='#f50' v-if='text==2'>已审批</a-tag>
        </span>
        <span slot='action' slot-scope='text, record'>
          <template>
            <a @click='startProcess(record)' v-if='record.checkState==0'>提交审批</a>
            <a @click='completeTask(record)' v-if="record.taskDefinitionKey&&record.taskDefinitionKey=='__initiator__'">提交审批</a>
             <a-divider type='vertical' v-if="record.checkState==0||(record.taskDefinitionKey&&record.taskDefinitionKey=='__initiator__')" />
            <a @click="$refs.drawer.showDrawer(record,'form1')" v-if="record.checkState==0||record.taskDefinitionKey=='__initiator__'">编辑</a>
            <a-divider type='vertical' v-if="record.checkState==0||record.taskDefinitionKey=='__initiator__'" />
             <a @click="$refs.drawer.showDrawer(record,'form2')">查看</a>
             <a-divider type='vertical' />
          </template>
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item v-if="record.checkState==0||record.taskDefinitionKey=='__initiator__'">
                <a href='javascript:;' @click='$refs.table.del(record)'>删除</a>
              </a-menu-item>
                <a-menu-item>
                <a href='javascript:;' @click='printPayment(record)'>资金支付单</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <expense-drawer ref='drawer'></expense-drawer>

    </a-card>
    <upload-modal ref='upload' tip='只能上传.xls,.xlsx文件，且不超过512K' :action='api.expense.uploadFile' accept='.xls,.xlsx'
                  @change='uploadSuccess'></upload-modal>
    <print-modal ref='printModal'></print-modal>
    <process-image ref='processImg' v-if='processImageVisible' :visible.sync='processImageVisible' :processInstanceId='processInstanceId'></process-image>
  </div>
</template>
<script>
import expenseDrawer from "./expense_drawer.vue"
import ProcessImage from "../../flowable/process/process_image"
import api from "../js/api"
import PrintModal from "@/views/module/business/order/print/print_modal"

export default {
  name: "ExpenseList",
  components: {
    expenseDrawer,
    ProcessImage,
    PrintModal
  },
  data() {
    let me = this
    //操作按钮
    let operator = []
    operator.push({
      name: "添加",
      icon: "plus",
      that: this,
      handler: function(table, that) {
        that.$refs.drawer.showDrawer({}, "form1")
      }
    })
    operator.push({
      name: "导入费用",
      icon: "upload",
      that: this,
      handler: function(table, that) {
        that.$refs.upload.open({ companyCode: that.companyCode })
      }
    })
    operator.push({
      name: "删除",
      icon: "delete",
      batch: true,
      handler: function(table) {
        table.remove()
      }
    })
    return {
      api: Object.assign(api, this.api),
      processImageVisible: false,
      //列表初始化查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: "序号",
          width: "60",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "姓名",
          align: "center",
          dataIndex: "saleManName"
        },
        {
          title: "部门名称",
          align: "center",
          dataIndex: "deptName"
        },
        {
          title: "款项类型",
          align: "center",
          dataIndex: "expenseType",
          scopedSlots: { customRender: "paymentType" }
        },
        {
          title: "申请金额",
          align: "center",
          dataIndex: "expenseAmount"
        },
        {
          title: "申请日期",
          align: "center",
          dataIndex: "payDate"
        },
        {
          title: "款项说明",
          align: "center",
          dataIndex: "expense"
        },
        {
          title: "备注",
          align: "center",
          dataIndex: "remark"
        },
        {
          title: "创建日期",
          align: "center",
          dataIndex: "createTime"
        },
        {
          title: "审批状态",
          align: "center",
          dataIndex: "checkState",
          fixed: "right",
          width: 100,
          scopedSlots: { customRender: "checkState" }
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          fixed: "right",
          width: 300,
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator
    }
  },
  created() {
    this.$refs.searchs.searchHandler()
  },
  methods: {
    onLoad() {
      this.$refs.searchs.searchHandler()
    },
    uploadSuccess() {
      // 上传成功
      this.onLoad()
      this.$refs.upload.close()
      this.$message.success("导入成功")
    },
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    },
    //重新提交
    completeTask(consumption) {
      this.http.confirm(this, {
        content: "确认提交审批吗?",
        url: api.tasktodo.complete,
        requestBody: true,
        data: {
          taskId: consumption.taskId
        },
        success: function(me, data) {
          me.$refs.table.refresh()
        }
      })
    },
    //打印退款单
    printPayment(record) {
      this.$refs["printModal"].open(record.expenseId, 2, "Other")
    },
    startProcess(consumption) {
      this.http.confirm(this, {
        content: "确认提交审批吗?",
        url: api.processInstance.start,
        requestBody: true,
        data: {
          handlerName: "consumptionStartProcessHandler",
          businessKey: consumption.expenseId
        },
        success: function(me, data) {
          me.$refs.table.refresh()
        }
      })
    }
  }
}
</script>
